package cn.service.Impl;

import cn.service.ProcessService;
import org.jbpm.api.NewDeployment;
import org.jbpm.api.ProcessDefinition;
import org.jbpm.api.ProcessDefinitionQuery;
import org.jbpm.api.ProcessEngine;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.zip.ZipInputStream;

/**
 * Created by Administrator on 2017/4/20.
 */
@Service
@Transactional
public class ProcessServiceImpl implements ProcessService {
    @Resource
    private ProcessEngine processEngine;

    /**
     * 查询所有最新的流程定义列表
     *
     * @return
     */
    public List findAll() {
        //查询所有流程定义
        ProcessDefinitionQuery processDefinitionQuery = processEngine.getRepositoryService().createProcessDefinitionQuery();
        //对流程定义进行排序，使用自带的静态属性对Version进行排序
        processDefinitionQuery.orderAsc(ProcessDefinitionQuery.PROPERTY_VERSION);
        //获得所有的流程定义
        List<ProcessDefinition> list = processDefinitionQuery.list();
        //声明一个Map集合
        Map<String, ProcessDefinition> map = new HashMap<String, ProcessDefinition>();
       /*
       循环便利出所有的流程定义
       由于之前根据version排序，所以相同key，version越大则越靠后
       然后进行赋值,例如：
       key=1,version=1
       key=1,version=2
       由于key值相同，存入map集合会自动覆盖之前的
       然后得出来的就是最新的版本
        */
        for (ProcessDefinition pd : list) {
            map.put(pd.getKey(), pd);
        }
        return new ArrayList<ProcessDefinition>(map.values());
    }

    /**
     * 部署流程
     *
     * @param file
     */
    public void add(File file) {
        //获得部署对象
        NewDeployment deployment = processEngine.getRepositoryService().createDeployment();
//        deployment.addResourceFromFile(file);
//        System.out.println(deployment.deploy());
        try {
            FileInputStream fileInputStream = new FileInputStream(file);
            ZipInputStream zipInputStream = new ZipInputStream(fileInputStream);
            deployment.addResourcesFromZipInputStream(zipInputStream);
            System.out.println(deployment.deploy());
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }

    /**
     * 删除流程定义
     */
    public void delete(String key) {
        //查询出所有的流程
        ProcessDefinitionQuery query = processEngine.getRepositoryService().createProcessDefinitionQuery();
        //添加查询条件，根据key来查询
        query.processDefinitionKey(key);
        //用集合接受筛选后的数据
        List<ProcessDefinition> list = query.list();
        //循环删除
        for (ProcessDefinition lists : list) {
            processEngine.getRepositoryService().deleteDeployment(lists.getDeploymentId());
        }
    }

    /**
     * 根据流程定义的id获取此流程定义对应的图片输入流
     *
     * @param id
     * @return
     */
    @Override
    public InputStream getImageInputStream(String id) {
        ProcessDefinitionQuery query = processEngine.getRepositoryService().createProcessDefinitionQuery();
        query.processDefinitionId(id);
        ProcessDefinition pd = query.uniqueResult();
        String name = pd.getImageResourceName();
        InputStream inputStream = processEngine.getRepositoryService().getResourceAsStream(pd.getDeploymentId(), name);
        return inputStream;
    }
}
